package cn.java79.springboot.zsb.util;

import cn.java79.springboot.zsb.config.OssConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@Component
public class TentcentOssUtil {
    public static void main(String[] args) throws  Exception {
//        String s = uploadFile(new File("D:\\Documents\\图片和资料\\AI.png"), "zsb/");
//        System.out.println(s);

        String s = uploadFile2(new FileInputStream("D:\\Documents\\图片和资料\\AI.png"),"ai.jpg", "zsb/");
        System.out.println(s);
    }

    public static String uploadFile(File file, String dir) {
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "AKIDBBCIHF6rPsCJOK0QbZIn4CD3jDWdehrx";
        String secretKey = "6zhBQUJyhLuGNEqDzXwAFE2t4A2jFa7V";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-nanjing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传到的存储桶
        String bucketName = "mypictures-1251405045";
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String key = dir + UUID.randomUUID()+ "." + suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        // 上传文件。
        cosClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        cosClient.shutdown();
        Date expiration = new Date(System.currentTimeMillis()+1000*86400*365);
        URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
        String resultStr = "https://" +  url.getHost() + "/" + key;
        return resultStr;
    }
    public static String uploadFile2(InputStream in,String name, String dir) {
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "AKIDBBCIHF6rPsCJOK0QbZIn4CD3jDWdehrx";
        String secretKey = "6zhBQUJyhLuGNEqDzXwAFE2t4A2jFa7V";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-nanjing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传到的存储桶
        String bucketName = "mypictures-1251405045";
        String fileName =name;
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String key = dir + UUID.randomUUID()+ "." + suffix;
        ObjectMetadata metadata=new ObjectMetadata();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, in,metadata);
        // 上传文件。
        cosClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        cosClient.shutdown();
        System.out.println(metadata.getRawMetadata());
        Date expiration = new Date(System.currentTimeMillis()+1000*86400*365);
        URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
        String resultStr = "https://" +  url.getHost() + "/" + key;
        return resultStr;
    }
    public static String uploadFile2(InputStream in,String name, String dir,OssConfig config) {
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = config.getSecid();
        String secretKey = config.getSeckey();
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(config.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传到的存储桶
        String bucketName = config.getBucketname();

        String key = dir +name;
        ObjectMetadata metadata=new ObjectMetadata();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, in,metadata);
        // 上传文件。
        cosClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        cosClient.shutdown();
        System.out.println(metadata.getRawMetadata());
        Date expiration = new Date(System.currentTimeMillis()+1000*86400*365);
        URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
        String resultStr = "https://" +  url.getHost() + "/" + key;
        return resultStr;
    }
    public static String uploadFile(File file, String dir, OssConfig config) {
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = config.getSecid();
        String secretKey = config.getSeckey();
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region(config.getRegion());
        ClientConfig clientConfig = new ClientConfig(region);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        // 指定要上传到的存储桶
        String bucketName =config.getBucketname();
        String fileName = file.getName();
        String suffix = fileName.substring(fileName.lastIndexOf('.') );
        String key = dir + UUID.randomUUID()+ suffix;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);

        // 上传文件。
        cosClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        cosClient.shutdown();
        Date expiration = new Date(System.currentTimeMillis()+1000*86400*365);
        URL url = cosClient.generatePresignedUrl(bucketName, key, expiration);
        String resultStr = "https://" +  url.getHost() + "/" + key;
        return resultStr;
    }
}
